package com.juggernaut;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Id;
import org.apache.zookeeper.data.Stat;
import org.apache.zookeeper.server.auth.DigestAuthenticationProvider;

/**
 * Hello world!
 * 
 */
public class SimpleTestZookeeper {

    private static String connectString = "localhost:2181";

    private static int sessionTimeout = 30000;

    public static void main(String[] args) throws Exception {
        final CountDownLatch sp = new CountDownLatch(1);
        ZooKeeper zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {

            @Override
            public void process(WatchedEvent event) {
                System.out.println("watch被触发执行了！");
                sp.countDown();
            }
        });
        sp.await();

        Stat exists = zk.exists("/zjh", false);
        System.out.println(exists);

        byte[] data = zk.getData("/zjh", false, null);

        System.out.println(new String(data));

        zk.setData("/zjh", "zjhdd".getBytes(), 0);
    }

    // 权限问题
    public static void authTest() throws IOException, NoSuchAlgorithmException, KeeperException, InterruptedException {
        List<ACL> acls = new ArrayList<ACL>();

        // 添加第一个id，采用用户名密码形式
        Id id1 = new Id("digest", DigestAuthenticationProvider.generateDigest("admin:admin"));
        ACL acl1 = new ACL(ZooDefs.Perms.ALL, id1);
        acls.add(acl1);

        // 添加第二个id，所有用户可读权限
        Id id2 = new Id("world", "anyone");
        ACL acl2 = new ACL(ZooDefs.Perms.READ, id2);
        acls.add(acl2);

        // Zk用admin认证，创建/test ZNode。
        ZooKeeper zk = new ZooKeeper(connectString, sessionTimeout, null);
        zk.addAuthInfo("digest", "admin:admin".getBytes());
        zk.create("/test", "data".getBytes(), acls, CreateMode.PERSISTENT);
    }

    public static void delete(String path) throws IOException, KeeperException, InterruptedException {
        ZooKeeper zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {

            @Override
            public void process(WatchedEvent event) {

            }
        });

        delRec(path, zk);

        System.out.println(zk.getChildren("/", false));
    }

    private static void delRec(String rootNodeName, ZooKeeper zk) throws KeeperException, InterruptedException {
        if (zk.exists(rootNodeName, false) == null) {
            return;
        }
        String tempNode = null;
        List<String> nodeList = zk.getChildren(rootNodeName, false);
        for (String nodeName : nodeList) {
            tempNode = rootNodeName + "/" + nodeName;

            List<String> childList = zk.getChildren(tempNode, false);
            if (childList.size() >= 0) {
                delRec(tempNode, zk);
            }
        }

        zk.delete(rootNodeName, -1);
        System.out.println("delete:" + rootNodeName);
    }
}
